package com.kaizzi.data.jdl.helper;

import com.kaizzi.data.common.been.TempBeen;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.service.MotionClocksService;
import freemarker.core.TemplateCombinedMarkupOutputModel;
import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/16
 */
@Component
public class PageData38 {

    @Resource
    private MotionClocksService motionClocksService;

    public String getData(ExcelModel model){

        List<String> dateList = DateUtil.getDateList(2);
        List<TempPage38> tempPage38s = new ArrayList<>();
        dateList.forEach(date -> {
            List<String> transTime = DateUtil.getTransTime(date);
            List<TempBeen> clockInSportType = motionClocksService.getClockInSportType(transTime.get(0), transTime.get(1));

            TempPage38 tempPage38 = new TempPage38();
            List<TempBeen2> been2s = new ArrayList<>();
            clockInSportType.forEach(sportType -> {
                TempBeen2 been2 = new TempBeen2();
                been2.setName(sportType.getTempName());
                been2.setCount(Long.parseLong(sportType.getTempData()));
                been2.setUsers(sportType.getTempCount());
                been2.setAvg((int)Math.floor((double) been2.getCount()/(double) been2.getUsers()));
                been2s.add(been2);
            });
            tempPage38.setDate(date);
            tempPage38.setBeens(been2s);
            tempPage38s.add(tempPage38);
        });

        Page38 page38 = new Page38();
        page38.setClockInCount(tempPage38s);
        page38.setDataPeriod(DateUtil.getLastWeekDate());
        page38.setTitle(LogicUtil.getTitle(model,38));

        List<String> dateList1 = DateUtil.getDateList(1);
        List<String> transTime = DateUtil.getTransTime(dateList1.get(0));
        page38.setUserAge(motionClocksService.getClockInAge(transTime.get(0),transTime.get(1)));
        List<TempBeen> clockInArea = motionClocksService.getClockInArea(transTime.get(0), transTime.get(1));

        long sum = motionClocksService.getClockInArea4(transTime.get(0), transTime.get(1));
        TempBeen tempBeen = getTempBeen(sum,clockInArea);
        clockInArea.add(tempBeen);
        clockInArea.sort(Comparator.comparing(TempBeen::getTempCount).reversed());
        page38.setUserArea(clockInArea);

        page38.setUserGender(motionClocksService.getClockInGender(transTime.get(0),transTime.get(1)));

        return JsonUtil.serialize(page38);
    }

    private TempBeen getTempBeen(long sum, List<TempBeen> clockInArea) {
        long sum1 = clockInArea.stream().mapToLong(TempBeen::getTempCount).sum();
        TempBeen tempBeen = new TempBeen();
        tempBeen.setTempName("其他");
        tempBeen.setTempCount(sum-sum1);
        return tempBeen;
    }

    @Data
    private static class TempBeen2{
        private String name;
        private long count;
        private long users;
        private int avg;
    }

    @Data
    private static class TempPage38{
        private String date;
        private List<TempBeen2> beens;
    }

    @Data
    private static class Page38{
        private String title;
        private String dataPeriod;
        private List<TempPage38> clockInCount;
        private List<TempBeen> userArea;
        private List<TempBeen> userGender;
        private List<TempBeen> userAge;
    }
}
